﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Etapa1.ModelDescription
{
    public class CreateNormalsModel : Modelo
    {
        public CreateNormalsModel(List<VertexPositionNormalTexture> listaVertices, List<int> indices)
            : base(listaVertices, indices)
        {
        }

        public void CreateNormals()
        {
            foreach (var item in triangulos)
            {
                item.CalculateNormal();
            }

            for (int i = 0; i < vertices.Count; i++)
            {
                Vector3 normal = Vector3.Zero;
                int j;
                for (j = 0; j < vertices[i].TriangulosIn.Count; j++)
                {
                    normal += vertices[i].TriangulosIn[j].Normal;
                }
                normal = normal/ (j);
                normal.Normalize();
                VertexPositionNormalTexture vn = new VertexPositionNormalTexture();
                vn.Position = vertices[i].VertexInfo.Position;
                vn.TextureCoordinate = vertices[i].VertexInfo.TextureCoordinate;
                vn.Normal = normal;
                vertices[i].VertexInfo = vn;
            }

        }
    }
}
